package com.aaa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.aaa.dao.TesterSasDao;
import com.aaa.dao.TesterSdsDao;
import com.aaa.dao.ViewTestResultSasDao;
import com.aaa.dao.ViewTestResultSdsDao;
import com.aaa.entity.*;
import com.aaa.service.TestResultSdsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @projectName: 20220622springmvc
 * @package: com.aaa.service.impl
 * @className: TestResultSdsServiceImpl
 * @author: HH
 * @description: TODO
 * @date: 2022/7/18 20:12
 * @version: 1.0
 */
@Service
public class TestResultSdsServiceImpl implements TestResultSdsService {
    @Autowired
    private TesterSdsDao testerSdsDao;
    @Autowired
    private ViewTestResultSdsDao viewTestResultSdsDao;
    @Override
    public String saveTestResult(List<Map<String,Object>> answerList, TesterVo tester) {
        //保存测试者信息
        TesterFpa testerFpa= new TesterFpa();
        //Bean之间的转换主要是相同属性的复制
        /**
         * 复制Bean对象属性<br>
         * 限制类用于限制拷贝的属性，例如一个类我只想复制其父类的一些属性，就可以将editable设置为父类
         *
         * @param source           源Bean对象
         * @param target           目标Bean对象
         * @param ignoreProperties 不拷贝的的属性列表
         */
        BeanUtil.copyProperties(tester,testerFpa,"id");
        testerSdsDao.insert(testerFpa);
        //获取自增之后的主键
        Integer testerId = testerFpa.getId();
        //保存测试结果信息
        int i = this.insertBatch(answerList, testerId);
        if(i>0){
            return this.seleResult(testerId);
        }
        return null;
    }

    @Override
    public int insertBatch(List<Map<String,Object>> answerList, Integer testerId) {
        return testerSdsDao.insertBatch(answerList,testerId);
    }
    @Override
    public String seleResult(Integer testerId) {

        ViewTestResultSds viewSds = viewTestResultSdsDao.selectResultSas(testerId);
        double a = viewSds.getForward() + viewSds.getInversion();
        if (a <= 53) {
            return "/views/front/SdsNormal.jsp";
        } else if (a < 62) {
            return "/views/front/SdsMild.jsp";
        } else if (a < 72) {
            return "/views/front/SdsModerate.jsp";
        } else {
            return "/views/front/SdsSevere.jsp";
        }
    }
}
